<%--
  Created by IntelliJ IDEA.
  User: hp
  Date: 2019/1/14
  Time: 21:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>会员类型管理</title>
    <jsp:include page="${pageContext.request.contextPath}/common/tag.jsp"/>
</head>
<script>
    $(function () {
        $("#membertable").bootstrapTable({
            url : '${pageContext.request.contextPath}/membertype/query',
            height : 510,  //表格高度
            pagination : true, //开启分页
            striped : true,
            pageSize : 5, //每页显示的行数
            pageList:[2,4,8,16], //设置可供选择的页面数据条数
            pageNumber:1, //如果设置了分页，首页页码。
            sidePagination:'server',  //启用服务器端分页
            queryParams:function(params){
                var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                    limit: params.limit,  //页面大小
                    offset: params.offset, //页码
                };
                return temp;
            },
            columns : [
                {checkbox:true},
                {field:'id',title:'会员编号',align:'center'},
                {field:'type',title:'会员类型',align:'center'},
                {field:'zhekou',title:'折扣',align:'center'},
                {field:'fenshu',title:'定义积分',align:'center'},
                {title:'操作',align:'center',
                    formatter: function (value,row,index) {
                        return  "<button class=\"btn btn-primary\" data-toggle=\"modal\" href=\"#addmodal\" onclick='huoqu("+index+")'><span class=\"glyphicon glyphicon-edit\"></span>修改</button>&nbsp;&nbsp;<button class=\"btn btn-primary\" onclick='shanchu("+row.id+")'><span class=\"glyphicon glyphicon-trash\"></span>&nbsp;&nbsp 删除</button>";
                    }
                }
            ]
        });

        <!--校验框架-->
        $('#addfrom').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                /* validating: 'glyphicon glyphicon-refresh'*/
            },
            fields: {
                type: {
                    message: '请输入会员类型',
                    validators: {
                        notEmpty: {
                            message: '会员类型不能为空！'
                        }
                    }
                },
                zhekou:{
                    message:'请输入会员折扣',
                    validators:{
                        notEmpty:{
                            message:'会员折扣不能为空！'
                        },
                        numeric : {
                            message:'输入必须数数字！'
                        }
                    }
                },
                fenshu:{
                    message:'请输入会员定义积分',
                    validators:{
                        notEmpty:{
                            message:'会员定义积分不能为空！'
                        },
                        digits : {
                            message:'输入必须是整数！'
                        }
                    }
                }
            }
        });


        $("#tianjia").click(function () {
            document.getElementById("addfrom").reset();
            $("#addfrom").data('bootstrapValidator').resetForm();
            $("#id").val(0);
            $("#jiaxiu").val(1);
        })
    })
</script>
<body style="margin:20px 10px;overflow-y: hidden">
    <div>
        <button class="btn btn-primary" data-toggle="modal" href="#addmodal" id="tianjia">添加</button>
        <table id="membertable"></table>

        <div class="modal fade" id="addmodal">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title">添加会员类型</h4>
                    </div>
                    <div class="modal-body">
                        <form action="" id="addfrom" method="post" role="form">
                            <input type="hidden" name="id" id="id" value="0">
                            <div class="form-group">
                                <label for="type" class="control-label">会员类型：</label>
                                <input type="text" class="form-control" name="type" id="type" placeholder="请输入会员类型">
                            </div>
                            <div class="form-group">
                                <label for="zhekou" class="control-label">会员折扣：</label>
                                <input type="text" class="form-control" name="zhekou" id="zhekou" placeholder="请输入会员折扣">
                            </div>
                            <div class="form-group">
                                <label for="fenshu" class="control-label">定义积分</label>
                                <input type="text" class="form-control" name="fenshu" id="fenshu" placeholder="定义会员类型">
                            </div>
                            <input type="hidden" id="jiaxiu" value="1">
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary" onclick="baocun()">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
    </div>
</body>
<script>

    function huoqu(index) {
        $("#membertable").bootstrapTable("uncheckAll");
        $("#membertable").bootstrapTable("check",index);
        var rows = $("#membertable").bootstrapTable('getSelections')[0];
        console.log(rows);
        $("#jiaxiu").val(2);
        $("#id").val(rows.id);
        $("#type").val(rows.type);
        $("#zhekou").val(rows.zhekou);
        $("#fenshu").val(rows.fenshu);
    }
    function shanchu(id) {
        swal({
            title: "您确定要删除这条信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            closeOnConfirm: false
        },function () {
            $.post("${pageContext.request.contextPath}/membertype/delete",{"id":id},function (data) {
                /*从新绑定表格*/
                $("#membertable").bootstrapTable('load',data);
                swal("提示信息","修改成功","success");
            })
        });

    }

    function baocun() {
        var jiaxiu = $("#jiaxiu").val();
        if(jiaxiu==1){
            $("#addfrom").data("bootstrapValidator").validate();
            var flag = $("#addfrom").data("bootstrapValidator").isValid() ;
            if(flag) {
                var formStr = $("#addfrom").serialize();
                //处理表单控件的中文乱码
                params = decodeURIComponent(formStr, true);
                $.ajax({
                    type:'post',
                    url:'${pageContext.request.contextPath}/membertype/insert',
                    data:params,
                    success:function (data) {
                        document.getElementById("addfrom").reset();
                        $("#addfrom").data('bootstrapValidator').resetForm();
                        $("#addmodal").modal('hide');
                        /*从新绑定表格*/
                        $("#membertable").bootstrapTable('load',data);
                        swal("提示信息","添加成功","success");
                    }
                });
            }
        }else{
            $("#addfrom").data("bootstrapValidator").validate();
            var flag = $("#addfrom").data("bootstrapValidator").isValid() ;
            if(flag) {
                var formStr = $("#addfrom").serialize();
                //处理表单控件的中文乱码
                params = decodeURIComponent(formStr, true);
                $.ajax({
                    type:'post',
                    url:'${pageContext.request.contextPath}/membertype/update',
                    data:params,
                    success:function (data) {
                        document.getElementById("addfrom").reset();
                        $("#addfrom").data('bootstrapValidator').resetForm();
                        $("#addmodal").modal('hide');
                        /*从新绑定表格*/
                        $("#membertable").bootstrapTable('load',data);
                        swal("提示信息","修改成功","success");
                    }
                });
            }
        }
    }
</script>
</html>
